﻿using DevComponents.AdvTree;
using DevComponents.DotNetBar;
using SDATA;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SABI
{
    public partial class frmHOME : Form
    {
        public frmHOME()
        {
            InitializeComponent();
        }

        private void frmHOME_Load(object sender, EventArgs e)
        {
            //this.WindowState = FormWindowState.Maximized;
            //SplitMain.SplitterDistance = 250;
            //AddTab(new frmStartup(), false);
            //FRM.loadDATA_Combo();            
        }

        private bool CheckTab(Form frm)
        {
            if (TabMain.Tabs.Count > 0)
            {
                for (int i = 0; i <= TabMain.Tabs.Count - 1; i++)
                {
                    if (TabMain.Tabs[i].Name == frm.Name)
                    {
                        TabMain.SelectedTabIndex = i;
                        return true;
                    }
                }
            }
            return false;
        }

        public void AddTab(Form frm, bool closebtnVisible)
        {
            //kiểm tra trước khi add thêm
            if (CheckTab(frm))
                return;
            SuperTabItem newtab = TabMain.CreateTab("Tab");
            frm.TopLevel = false;
            frm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            frm.Dock = DockStyle.Fill;
            frm.Show();
            newtab.Name = frm.Name;
            newtab.Text = frm.Text;
            newtab.CloseButtonVisible = closebtnVisible;
            newtab.AttachedControl.Controls.Add(frm);
            TabMain.SelectedTabIndex = TabMain.Tabs.Count - 1;
        }

        public void AddTab(Form frm)
        {
            //kiểm tra trước khi add thêm
            if (CheckTab(frm))
                return;
            SuperTabItem newtab = TabMain.CreateTab("Tab");
            frm.TopLevel = false;
            frm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            frm.Dock = DockStyle.Fill;
            frm.Show();
            newtab.Name = frm.Name;
            newtab.Text = frm.Text;
            newtab.AttachedControl.Controls.Add(frm);
            TabMain.SelectedTabIndex = TabMain.Tabs.Count - 1;
        }

        public void AddTab(string strfrm)
        {
            strfrm = Assembly.GetEntryAssembly().GetName().Name + "." + strfrm;
            Form frm = (Form)Assembly.GetEntryAssembly().CreateInstance(strfrm);
            if (frm == null)
            {
                MessageBox.Show("Form: " + strfrm + " không tồn tại!");
                return;
            }
            if (CheckTab(frm))
                return;
            SuperTabItem newtab = TabMain.CreateTab("Tab");
            frm.TopLevel = false;
            frm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            frm.Dock = DockStyle.Fill;
            frm.Show();
            newtab.Name = frm.Name;
            newtab.Text = frm.Text;
            newtab.AttachedControl.Controls.Add(frm);
            TabMain.SelectedTabIndex = TabMain.Tabs.Count - 1;
        }

        public void CloseTab(Form frm)
        {
            if (TabMain.Tabs.Count > 0)
            {
                for (int i = 0; i <= TabMain.Tabs.Count - 1; i++)
                {
                    if (TabMain.Tabs[i].Name == frm.Name)
                    {
                        TabMain.Tabs.Remove(frm.Name);
                        return;
                    }
                }
            }
        }

        private void SplitMain_SplitterMoved(object sender, SplitterEventArgs e)
        {
            //if (SplitMain.SplitterDistance < 250)
            //{
            //    SplitMain.SplitterDistance = 250;
            //}
            if (SplitMain.SplitterDistance > 400)
                SplitMain.SplitterDistance = 400;
        }

        private void advTreeQTHT_NodeDoubleClick(object sender, DevComponents.AdvTree.TreeNodeMouseEventArgs e)
        {
            if (e.Node.TagString != "")
                AddTab(e.Node.TagString);
        }

        private void advTreeNVBH_NodeDoubleClick(object sender, DevComponents.AdvTree.TreeNodeMouseEventArgs e)
        {
            if (e.Node.TagString != "")
                AddTab(e.Node.TagString);
        }
        
        private void advTreeDMHT_NodeDoubleClick(object sender, DevComponents.AdvTree.TreeNodeMouseEventArgs e)
        {
            if (e.Node.TagString != "")
                AddTab(e.Node.TagString);
        }

        private void frmHOME_Shown(object sender, EventArgs e)
        {
            loadGUI_Form_Menu();
            Application.DoEvents();

            loadGUI_Form();            

            this.WindowState = FormWindowState.Maximized;
            SplitMain.SplitterDistance = 250;
            AddTab(new frm0000(), false);
            FRM.loadDATA_Combo();            
        }

        public void loadGUI_Form()
        {
            string[] arrDATAinfo = FRM.DataInfo.Split('|');
            if (arrDATAinfo.Length == 5)
            {
                FRM.Server = arrDATAinfo[1];
                FRM.Database = arrDATAinfo[2];
            }
            txtServer.Text = FRM.Server;
            txtDatabase.Text = FRM.Database;
            txtDivision.Text = FRM.DivisionID;
            txtTime.Text = FRM.Period;
            txtUser.Text = FRM.UserName;
            txtGroup.Text = FRM.GroupID;
            txtDateDue.Text = string.Format("{0:"+ FRM.SystemDateFormat +"}", FRM.DateDue);

        }

        public void loadGUI_Form_Menu()
        {
            loadGUI_NodeMenu(advTreeQTHT, btnQTHT, "1000");
            loadGUI_NodeMenu(advTreeDMHT, btnDMHT, "2000");
            loadGUI_NodeMenu(advTreeNVBH, btnNVBH, "3000");
            loadGUI_NodeMenu(advTreeQLHK, btnQLHK, "4000");
            loadGUI_NodeMenu(advTreeTSCD, btnTSCD, "5000");
            loadGUI_NodeMenu(advTreeQLDH, btnQLDH, "6000");
            loadGUI_NodeMenu(advTreeGTSP, btnGTSP, "7000");
            loadGUI_NodeMenu(advTreeNSTL, btnNSTL, "8000");
            loadGUI_NodeMenu(advTreeHTBC, btnHTBC, "9000");
        }

        private void tiktak_Tick(object sender, EventArgs e)
        {
            txtNow.Text = string.Format("{0}", DateTime.Now);
            Application.DoEvents();
        }

        private void btnLogout_Click(object sender, EventArgs e)
        {
            FRM.IsLogin = false;
            this.Close();            
        }

        private void frmHOME_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (FRM.IsLogin) { FRM.IsClose = true; }
        }

        private void txtDateDue_Click(object sender, EventArgs e)
        {
            AddTab(new frm0002(), true);
        }

        private void TabMain_SelectedTabChanged(object sender, SuperTabStripSelectedTabChangedEventArgs e)
        {
            if (TabMain.Tabs.Count > 1)
                Text = "SABI - " + TabMain.SelectedTab.Text;
            else
                Text = "SABI - Công ty Tư Vấn Quản Lý MEKONG";
        }

        private void advTreeQLHK_NodeDoubleClick(object sender, DevComponents.AdvTree.TreeNodeMouseEventArgs e)
        {
            if (e.Node.TagString != "")
                AddTab(e.Node.TagString);
        }

        private void advTreeQLHK_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && advTreeQLHK.SelectedNodes.Count > 0)
                if (advTreeQLHK.SelectedNodes[0].TagString != "")
                    AddTab(advTreeQLHK.SelectedNodes[0].TagString);
        }

        private void advTreeQTHT_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && advTreeQTHT.SelectedNodes.Count > 0)
                if (advTreeQTHT.SelectedNodes[0].TagString != "")
                    AddTab(advTreeQTHT.SelectedNodes[0].TagString);
        }

        private void advTreeDMHT_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && advTreeDMHT.SelectedNodes.Count > 0)
                if (advTreeDMHT.SelectedNodes[0].TagString != "")
                    AddTab(advTreeDMHT.SelectedNodes[0].TagString);
        }

        private void advTreeNVBH_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && advTreeNVBH.SelectedNodes.Count > 0)
                if (advTreeNVBH.SelectedNodes[0].TagString != "")
                    AddTab(advTreeNVBH.SelectedNodes[0].TagString);
        }

        void loadGUI_NodeMenu(AdvTree adt, ButtonItem  btni, string taskID)
        {
            adt.Nodes.Clear();

            string sql = "";
            DataTable dt;

            sql = "SELECT ID,Name FROM MK0000 LEFT JOIN MK0001 ON MK0001.TaskID = MK0000.ID";
            sql += string.Format(" WHERE Disabled = 0 AND IsView = 1 AND GroupID = '{0}' AND ID = '{1}'", FRM.GroupID, taskID);
            dt = FRM.GetDataTable(sql);

            if (dt.Rows.Count == 0)
            {
                btni.Visible = false;
                return;
            }
            else
            {
                btni.Visible = true;
                btni.Text = dt.Rows[0]["Name"].ToString();
            }

            sql = "SELECT ID,Name,FormID FROM MK0000 LEFT JOIN MK0001 ON MK0001.TaskID = MK0000.ID";
            sql += string.Format(" WHERE Disabled = 0 AND IsView = 1 AND GroupID = '{0}' AND ParentID = '{1}'",FRM.GroupID,taskID);
            dt = FRM.GetDataTable(sql);
            
            for(int i=0; i<dt.Rows.Count;i++)
            {
                Node n = new Node();
                n.Name = dt.Rows[i]["ID"].ToString();
                n.Text = dt.Rows[i]["Name"].ToString();
                n.TagString = dt.Rows[i]["FormID"].ToString();
                adt.Nodes.Add(n);
                _DQ_NodeMenu(n);
            }

        }

        void _DQ_NodeMenu(Node n)
        {

            string sql = "";
            DataTable dt;

            sql = "SELECT ID,Name,FormID FROM MK0000 LEFT JOIN MK0001 ON MK0001.TaskID = MK0000.ID";
            sql += string.Format(" WHERE Disabled = 0 AND IsView = 1 AND GroupID = '{0}' AND ParentID = '{1}'", FRM.GroupID, n.Name);
            dt = FRM.GetDataTable(sql);
            if(dt.Rows.Count==0)
            {
                n.Image = SABI.Properties.Resources.add;
                return;
            }
            else
            {
                n.Image = SABI.Properties.Resources.foldermini;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Node n_new = new Node();
                    n_new.Name = dt.Rows[i]["ID"].ToString();
                    n_new.Text = dt.Rows[i]["Name"].ToString();
                    n_new.TagString = dt.Rows[i]["FormID"].ToString();
                    n.Nodes.Add(n_new);
                    _DQ_NodeMenu(n_new);                                        
                }
            }
        }

        private void advTreeTSCD_NodeDoubleClick(object sender, TreeNodeMouseEventArgs e)
        {
            if (e.Node.TagString != "")
                AddTab(e.Node.TagString);
        }

        private void advTreeTSCD_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && advTreeTSCD.SelectedNodes.Count > 0)
                if (advTreeTSCD.SelectedNodes[0].TagString != "")
                    AddTab(advTreeTSCD.SelectedNodes[0].TagString);
        }

        private void advTreeQLDH_NodeDoubleClick(object sender, TreeNodeMouseEventArgs e)
        {
            if (e.Node.TagString != "")
                AddTab(e.Node.TagString);
        }

        private void advTreeQLDH_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && advTreeQLDH.SelectedNodes.Count > 0)
                if (advTreeQLDH.SelectedNodes[0].TagString != "")
                    AddTab(advTreeQLDH.SelectedNodes[0].TagString);
        }

        private void advTreeGTSP_NodeDoubleClick(object sender, TreeNodeMouseEventArgs e)
        {
            if (e.Node.TagString != "")
                AddTab(e.Node.TagString);
        }

        private void advTreeGTSP_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && advTreeGTSP.SelectedNodes.Count > 0)
                if (advTreeGTSP.SelectedNodes[0].TagString != "")
                    AddTab(advTreeGTSP.SelectedNodes[0].TagString);
        }

        private void advTreeNSTL_NodeDoubleClick(object sender, TreeNodeMouseEventArgs e)
        {
            if (e.Node.TagString != "")
                AddTab(e.Node.TagString);
        }

        private void advTreeNSTL_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && advTreeNSTL.SelectedNodes.Count > 0)
                if (advTreeNSTL.SelectedNodes[0].TagString != "")
                    AddTab(advTreeNSTL.SelectedNodes[0].TagString);
        }

        private void advTreeHTBC_NodeDoubleClick(object sender, TreeNodeMouseEventArgs e)
        {
            if (e.Node.TagString != "")
                AddTab(e.Node.TagString);
        }

        private void advTreeHTBC_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && advTreeHTBC.SelectedNodes.Count > 0)
                if (advTreeHTBC.SelectedNodes[0].TagString != "")
                    AddTab(advTreeHTBC.SelectedNodes[0].TagString);
        }

    }
}
